/*
	视频列表
		视频详情
		视频添加
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var dictData = Cookies.getJSON('kplus_web_dict');

//dom
$(function(){
	layui.use(['layer', 'table', 'form'], function(){
		var layer = layui.layer;
		var table = layui.table;
		var form = layui.form;

		//ajax data
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken: adminLoginedInfo.aToken
		};
		//create table data
		function createTableData(data){
			var table_data_arr = [];
			for(var i = 0; i < data.length; i++){
				var table_data = {
					video_id: data[i].v_id,
					title: data[i].title['cn'],
					linkCn: data[i].link['cn'],
					linkTw: data[i].link['tw'],
					linkEn: data[i].link['en'],
					admin: dictData.admin[data[i].create_admin],
					create_time: formatTime(data[i].format_time),
					news_status: data[i].status
				};
				table_data_arr.push(table_data);
			}
			return table_data_arr;
		}
		//table 构建
		function createTable(tableData){
			table.render({
				elem: '#tableContent',
				limit: 30, //分页
				height: 680,
				page: true,
				cols:[[ //表头
					{field: 'video_id', title: '视频ID', sort: true, fixed: 'left', width:120},
					{field: 'title', title: '标题', sort: true},
					{field: 'linkCn', title: '视频链接(大陆)'},
					{field: 'linkTw', title: '视频链接(港台)'},
					{field: 'linkEn', title: '视频链接(国外)'},
					{field: 'admin', title: '创建者', width:110},
					{field: 'create_time', title: '创建时间'},
					{field: 'news_status', title: '状态', width:80,sort: true},
					{field: 'option', title: '操作', toolbar:'#toolbar'}
				]],
				data: tableData
			});
		}
		//获取视频
		function getVideos(query){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] }); 
			var _ajaxData = {};
			query ? _ajaxData = $.extend(ajaxData, query) : _ajaxData = ajaxData;
			$.ajax({
				url: BASE_SERVICEURL + URlS.video,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						//console.log(responseData.data);
						createTable(createTableData(responseData.data))
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						location.href = './500.html';
					}})
				}
			});
		}
		//toolbar 查看详情
		table.on('tool(video-list-table)', function(obj){
			var layEvent = obj.event;
			var data = obj.data;
			if(layEvent == 'seeDetail'){
				//console.log(obj)
				x_admin_show('查看详情', 'video-detail.html?v_id='+data.video_id+'', 1200, 800);
			};
		});
		//表单高级检索
		form.on('submit(search)', function(data){
			//console.log(data.field);
			getVideos(data.field)
			return false;
		});
		//拉取视频列表
		getVideos();








	});
})





